package com.example.shopping2.a_c_fragment.home.homevalue

import BaseActivity
import IFragment
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.shopping2.R
import com.example.shopping2.a_a_entity.BaseEntity
import com.example.shopping2.a_a_mvp.contract.HomeContract
import com.example.shopping2.a_a_mvp.model.models.HomeModel
import com.example.shopping2.a_a_mvp.presenter.presenters.HomePresenter
import com.example.shopping2.a_c_fragment.home.adapter.HomeAdaoter
import com.example.shopping2.a_c_fragment.home.entity.HomeGoodsEntity
import com.example.shopping2.a_c_fragment.main.LoginActivity
import com.example.shopping2.a_c_network.HttpCode

class ValueActivity : BaseActivity<HomePresenter>(), HomeContract.HomeView, IFragment {
    private val valueTool: Toolbar by lazy { findViewById<Toolbar>(R.id.value_tool) }
    private val valueRec: RecyclerView by lazy { findViewById<RecyclerView>(R.id.value_rec) }
    private val valueFx: CheckBox by lazy { findViewById<CheckBox>(R.id.value_fx) }
    private val valueCar: CheckBox by lazy { findViewById<CheckBox>(R.id.value_car) }
    private val valueAddcar: AppCompatButton by lazy { findViewById<AppCompatButton>(R.id.value_addcar) }
    private var inflate: View? = null
    private var boolean = false
    var mybro = MyBro()
    var valuess:HomeGoodsEntity.DataBean? = null
    override fun findlayout(): Int {
        return R.layout.activity_value
    }

    override fun initView() {
        //获取sp
        boolean = getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE).getBoolean(
            "log",
            false
        )
        //注册广播
        var intentfilter = IntentFilter()
        intentfilter.addAction("user_login")
        registerReceiver(mybro, intentfilter)
        mypre = HomePresenter(HomeModel(),this)

        //添加购物车
        valueAddcar.setOnClickListener {
            if (boolean) {
                //添加购物车
                //获取id
                var id =
                    getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE).getLong("id", 0)
                var carlist : ArrayList < HashMap < String, Any>> = ArrayList()
                var map: HashMap<String, Any> = HashMap()
                map.put("goodsCount", 1)
                map.put("goodsDesc", ""+valuess?.goods_desc)
                map.put("goodsIcon", ""+valuess?.goods_default_icon)
                map.put("goodsId", ""+valuess?.id)
                map.put("goodsPrice", ""+valuess?.goods_default_price)
                map.put("goodsSku", ""+valuess?.goods_default_sku)
                map.put("time", System.currentTimeMillis() / 1000)
                map.put("code",HttpCode.ADDCAR)
                map.put("userId",id)
                carlist.add(map)
                mypre.requestAll(carlist)

            } else {
                var loginint = Intent(this, LoginActivity::class.java)
                loginint.putExtra("type", "goodvalue")
                startActivity(loginint)
            }
        }


    }

    override fun initData() {

        var value = intent.getSerializableExtra("goods")as HomeGoodsEntity.DataBean

        valuess = value


        inflate = LinearLayout.inflate(this, R.layout.value_head, null)
        var head_img = inflate?.findViewById<ImageView>(R.id.head_img)
        var head_seckill = inflate?.findViewById<TextView>(R.id.head_seckill)
        var head_title = inflate?.findViewById<TextView>(R.id.head_title)
        var head_tex3 = inflate?.findViewById<TextView>(R.id.head_tex3)

        Glide.with(this).load(valuess?.goods_default_icon).into(head_img)
        head_title?.setText(valuess?.goods_desc)
        head_seckill?.setText(valuess?.goods_default_sku)
        head_tex3?.setText(valuess?.goods_default_sku)

        mypre = HomePresenter(HomeModel(), this)
        var list: ArrayList<HashMap<String, Any>> = ArrayList()
        var map: HashMap<String, Any> = HashMap()
        map.put("categoryId", "14")
        map.put("pageNo", 1)
        map.put("code", HttpCode.HOMEDATA)
        list.add(map)
        mypre.requestAll(list)

    }

    override fun showHomeData(baseEntity: BaseEntity) {
        if (baseEntity is HomeGoodsEntity) {
            var data = baseEntity.data
            var adapter = HomeAdaoter(data as ArrayList<HomeGoodsEntity.DataBean>)
            valueRec.adapter = adapter
            valueRec.layoutManager =
                StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            adapter.addHeaderView(inflate!!)

        }else if(baseEntity is BaseEntity){
            if (baseEntity.status .equals("0")){
                ToastUtils.showShort(""+baseEntity.message)
            }
        }

    }

    //-----------------------------------------广播接收者-------------------------------------
    inner class MyBro : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if (intent.action == "user_login") {
                var booleanExtra = intent.getBooleanExtra("login_dialog", false)
                if (booleanExtra) {

                    boolean =
                        getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE).getBoolean(
                            "log",
                            false
                        )
                } else {

                    boolean =
                        getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE).getBoolean(
                            "log",
                            false
                        )
                }


            }
        }

    }
}